'use strict';

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _class;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _radium = require('radium');

var _radium2 = _interopRequireDefault(_radium);

var _lightbiFrontUtils = require('lightbi-front-utils');

var _lightbiFrontWidget = require('lightbi-front-widget');

var _lightbiFrontProject = require('lightbi-front-project');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var RegisterForm = (0, _radium2.default)(_class = function (_React$Component) {
  _inherits(RegisterForm, _React$Component);

  function RegisterForm(props) {
    _classCallCheck(this, RegisterForm);

    var _this = _possibleConstructorReturn(this, (RegisterForm.__proto__ || Object.getPrototypeOf(RegisterForm)).call(this, props));

    _this.state = { isSubmit: false, errorMsg: "" };
    _this.onSubmit = _this.onSubmit.bind(_this);
    _this.getUniqueId = _this.getUniqueId.bind(_this);
    _this.getUniqueType = _this.getUniqueType.bind(_this);

    _this.afterSubmitReject = _this.afterSubmitReject.bind(_this);
    _this.afterSubmitResolved = _this.afterSubmitResolved.bind(_this);
    _this.gotoLogin = _this.gotoLogin.bind(_this);
    _this.jumpUri = _this.props.location.query['jumpUri'];
    return _this;
  }

  _createClass(RegisterForm, [{
    key: 'afterSubmitReject',
    value: function afterSubmitReject(error) {
      var _this2 = this;

      this.setState({ isSubmit: false, errorMsg: error });
      setTimeout(function () {
        _this2.setState({ errorMsg: null });
      }, 1000);
    }
  }, {
    key: 'afterSubmitResolved',
    value: function afterSubmitResolved(userVoucher) {
      _lightbiFrontUtils.LoginInfo.login(userVoucher.getEnterpriseId(), userVoucher.getUserId(), userVoucher.getJwt(), false, true);

      if (this.jumpUri == null || this.jumpUri.length == 0) (0, _lightbiFrontUtils.Goto)('/admin/projects', this.props.router);else (0, _lightbiFrontUtils.Goto)(_lightbiFrontUtils.Base64.decode(this.jumpUri), this.props.router);
    }
  }, {
    key: 'onSubmit',
    value: function onSubmit() {
      if (this.refs.enterprise.isValid() && this.refs.mobile.isValid() && this.refs.password.isValid() && this.refs.verifyCode.isValid()) {
        this.setState({ isSubmit: true });
        _lightbiFrontProject.EnterpriseController.register(true, this.refs.enterprise.getValue(), this.refs.mobile.getValue(), this.refs.password.getValue(), this.refs.verifyCode.getValue(), this.afterSubmitResolved, this.afterSubmitReject);
      }
    }
  }, {
    key: 'getUniqueId',
    value: function getUniqueId() {
      if (this.refs.mobile.isValid()) return this.refs.mobile.getValue();else return null;
    }
  }, {
    key: 'getUniqueType',
    value: function getUniqueType() {
      return _lightbiFrontProject.UniqueType.MOBILE;
    }
  }, {
    key: 'gotoLogin',
    value: function gotoLogin() {
      if (this.jumpUri == null || this.jumpUri.length == 0) this.props.router.push(_lightbiFrontUtils.IN_DEPLOY_URI + '/entrance/login');else this.props.router.push(_lightbiFrontUtils.IN_DEPLOY_URI + '/entrance/login?jumpUri=' + this.jumpUri);
    }
  }, {
    key: 'render',
    value: function render() {
      var buttonClass = "btn btn-primary" + (this.state.isSubmit ? " disabled " : "");
      var buttonDiv = null;
      if (this.state.isSubmit) buttonDiv = _react2.default.createElement(
        'button',
        { className: buttonClass, style: { marginRight: '5px', marginTop: '10px', minWidth: '120px' }, type: 'submit', onClick: this.onSubmit },
        '\u63D0\u4EA4\u4E2D',
        _react2.default.createElement('i', { className: 'fa fa-spinner fa-spin', style: { marginLeft: '5px' } })
      );else buttonDiv = _react2.default.createElement(
        'button',
        { className: buttonClass, style: { marginRight: '5px', marginTop: '10px', minWidth: '120px' }, type: 'submit', onClick: this.onSubmit },
        '\u6CE8\u518C'
      );

      return _react2.default.createElement(
        'div',
        { className: 'p-t-15' },
        _react2.default.createElement(
          'div',
          { style: { width: '100%', marginBottom: '10px' } },
          _react2.default.createElement(_lightbiFrontWidget.BoxInput, {
            ref: 'enterprise',
            placeholder: '\u6B63\u786E\u7684\u540D\u79F0',
            label: '\u4F01\u4E1A\u540D\u79F0',
            isReadOnly: this.state.isSubmit,
            validateFunc: _lightbiFrontUtils.enterpriseValidate
          })
        ),
        _react2.default.createElement(
          'div',
          { style: { width: '100%', marginBottom: '10px' } },
          _react2.default.createElement(_lightbiFrontWidget.BoxInput, {
            ref: 'mobile',
            placeholder: '\u6709\u6548\u624B\u673A\u53F7',
            label: '\u624B\u673A',
            isReadOnly: this.state.isSubmit,
            validateFunc: _lightbiFrontUtils.mobileValidate
          })
        ),
        _react2.default.createElement(
          'div',
          { style: { width: '100%', marginBottom: '10px' } },
          _react2.default.createElement(_lightbiFrontWidget.BoxInput, {
            ref: 'password',
            placeholder: '\u5BC6\u7801\u5927\u4E8E6\u4F4D',
            label: "密码",
            isReadOnly: this.state.isSubmit,
            validateFunc: _lightbiFrontUtils.registerPasswordValidate
          })
        ),
        _react2.default.createElement(
          'div',
          { style: { width: '100%', marginBottom: '15px' } },
          _react2.default.createElement(_lightbiFrontWidget.VerifyCodeInboxLabelInput, {
            name: '\u9A8C\u8BC1\u7801',
            ref: 'verifyCode',
            placeholder: '\u6536\u5230\u7684\u9A8C\u8BC1\u7801',
            label: '\u9A8C\u8BC1\u7801',
            isSubmit: this.state.isSubmit,
            validateFunc: _lightbiFrontUtils.verificationCodeValidate,
            getUniqueId: this.getUniqueId,
            getUniqueType: this.getUniqueType
          })
        ),
        _react2.default.createElement(
          'div',
          { className: 'm-t-10' },
          _react2.default.createElement(
            'a',
            { style: { cursor: 'pointer' }, onClick: this.gotoLogin },
            '\u5DF2\u6CE8\u518C? \u53BB\u767B\u5F55'
          )
        ),
        _react2.default.createElement(
          'div',
          { style: { marginTop: '10px', textAlign: 'center' } },
          _react2.default.createElement(
            'div',
            { style: { position: 'relative', display: 'inline-block' } },
            buttonDiv,
            !!this.state.errorMsg ? _react2.default.createElement(
              'label',
              { style: { left: '0px', bottom: '-30px', position: 'absolute', fontSize: '12px', color: '#f55753', display: 'block', textAlign: 'left', textOverflow: 'ellipsis', whiteSpace: 'nowrap', overflow: 'hidden' } },
              this.state.errorMsg
            ) : null
          )
        )
      );
    }
  }]);

  return RegisterForm;
}(_react2.default.Component)) || _class;

module.exports = RegisterForm;